<?php
		
		class Tools extends CI_Controller {
				
				function __construct(){
						parent::__construct();
				}
				
				function index() {
				
				}
				
				function _mkcsv() {
						$data = "1241524200 1234qwer HF-Server
1241524201 1234qwer HF-Server
1241524202 1234qwer HF-Server
1241524203 1234qwer HF-Server
1241524204 1234qwer HF-Server
1241524205 1234qwer HF-Server
1241524206 1234qwer HF-Server
1241524207 1234qwer HF-Server
1241524208 1234qwer HF-Server
1241524209 1234qwer HF-Server";
						$file = './demo_mt4.csv';
						if ( $data && $fp = fopen($file, 'wa+'))
						{
								fputs($fp, $data);
								fclose($fp);
						}
						die('done');
				}
				
				function test_gmail() {
						$this->load->library('email');
						$config['protocol'] 	= 'smtp';
						$config['smtp_host'] 	= 'ssl://smtp.googlemail.com';
						$config['smtp_user'] 	= 'risingdempire@gmail.com';
						$config['smtp_pass'] 	= '8425751f';
						$config['smtp_port'] 	= 465;
						$config['mailtype'] 	= 'html';
						$config['charset'] 		= 'utf-8';

						$this->email->initialize($config);
						$this->email->set_newline("\r\n");

						$this->email->from('risingdempire@gmail.com', 'risingdempire@gmail.com');
						$this->email->to('holy.works01@gmail');
						$this->email->bcc('quekchangchun@gmail.com');
						$this->email->subject('Email Test');
						$this->email->message('Testing the email class.');

						$this->email->send();

						echo $this->email->print_debugger();
				}
				
				function cron_lots_size($num=500) {	

						$host = '118.139.182.218';
						$port = 5555;
						$file = './checkpoint_mt4.csv';
						$sep  = ' ';
						$accts = array();
						$pag_start = $this->_get_indx();
						$pag_end   = $pag_start+(int)$num;
						
						echo "Processing <strong>$num</strong> account".($num>1?'s':'')." from <strong>#".($pag_start+1)."</strong> to <strong>#".($pag_end+1)."</strong>...<br />";
						$from 	= mktime(0,0,0,10,1,2011);
						$to 	= time()+24*60*60;
						echo 'from '.date("m/d/Y",$from)." to ".date("m/d/Y",$to)."<br />\n";
						
						$end_reached = true;
						
						if ( is_file($file) && $fp = fopen($file, 'r'))
						{
								$indx = 0;
								echo "<ol>\n";
								while ( !feof($fp))
								{
										$line = fgets($fp, 1024);
										$indx++;
										if($indx<=$pag_start) continue;
										
										$items = explode($sep, $line);
										if(!@$items[0]) continue;
										$end_reached = false;
										
										$data = $this->_mt4_tradingvolume($host, $port, @$items[0], $from, $to);
										
										if ( isset($data['size']))
										{
												if($data['size']==0) continue;
												$items2 = explode("\n", $data['size']);
												array_shift($items2);
												foreach($items2 as $item)
												{
													$items3 = explode(';', $item);
													if(!isset($accts[$items3[0]])) $accts[$items3[0]] = array('total_lot_sizes'=>0, 'count'=>0);
													$accts[$items3[0]]['total_lot_sizes'] += round($items3[6]/100,2);
													$accts[$items3[0]]['count']++;
												}
										}
										$this->_save_indx($indx);
										echo "<li>Account #".@$items[0]." <strong>parsed</strong>.</li>";
										if($indx>=$pag_end)  break;
								}
								if(feof($fp)) $end_reached = true;
								fclose($fp);
								echo "</ol>\n";
						}
						
						// print_r($accts);
						// die();
						
						if($end_reached) 
						{
								echo "All accounts <strong>parsed</strong>.<br />";
								$this->_save_indx(0);
						}
						else
						{
								echo "Refresh this page to process next accounts list.<br />";
						}
						
						$store = array();
						$data = @file_get_contents('./datastore.json');
						if($data)
						{
							$store = json_decode($data,true);
						}
						// if(!is_array($store)) $store = array();
						// print_r($store);
						// die();
						
						$new_items = array();
						$item_no = 0;
						foreach($accts as $login=>$val)
						{
								if($val['total_lot_sizes']>=100)
								{
										for($lot=$val['total_lot_sizes'];$lot>=100;$lot-=200)
										{
												$checkpoint = $this->_get_checkpoint($lot);
												if ( !isset($store[$login][$checkpoint]))
												{
													$item_no++;
													$info = $this->_mt4_info($host, $port, $login);
													$store[$login][$checkpoint] = array('checkpoint'=>$checkpoint, 'item_no'=>$item_no, 'date'=>date("m-d-Y"), 'time'=>date("H:i"), 'account_no'=>$login, 'traded_lot_size'=>$val['total_lot_sizes'], 'current_balance'=>@$info['balance'], '10p_current_balance'=>round((@$info['balance']*10)/100,2), 'leverage'=>@$info['leverage'], 'name'=>@$info['name'], 'country'=>@$info['country'], 'email'=>@$info['email']);
													$new_items[] = $store[$login][$checkpoint];
												}
										}
								}
						}
						
						if ( count($store)>0)
						{
							$json = json_encode($store);
							if ( $json && $fp = fopen('./datastore.json', 'wb+'))
							{
								fputs($fp, $json);
								fclose($fp);
							}
						}
						
						if ( count($new_items)>0)
						{
								$msg = '<h3>Checkpoints</h3>'."\n";
								$msg .= '<table cellpadding="2" cellspacing="3" border="1">'."\n";
								$msg .= '<tr>'."\n";
								$msg .= '<th>#</th>'."\n";
								$msg .= '<th>Checkpoint</th>'."\n";
								$msg .= '<th>Date</th>'."\n";
								$msg .= '<th>Time</th>'."\n";
								$msg .= '<th>Account #</th>'."\n";
								$msg .= '<th>Name</th>'."\n";
								$msg .= '<th>Country</th>'."\n";
								$msg .= '<th>E-mail</th>'."\n";
								$msg .= '<th>Traded Lot Size</th>'."\n";
								$msg .= '<th>Current Balance</th>'."\n";
								$msg .= '<th>Leverage</th>'."\n";
								$msg .= '<th>10% of Current Balance</th>'."\n";
								$msg .= '</tr>'."\n";
								foreach($new_items as $item) 
								{
									$msg .= '<tr>'."\n";
									$msg .= '<td>'.$item['item_no'].'</td>'."\n"; 
									$msg .= '<td>'.$item['checkpoint'].'</td>'."\n";
									$msg .= '<td>'.$item['date'].'</td>'."\n";
									$msg .= '<td>'.$item['time'].'</td>'."\n";
									$msg .= '<td>'.$item['account_no'].'</td>'."\n";
									$msg .= '<td>'.$item['name'].'</td>'."\n";
									$msg .= '<td>'.$item['country'].'</td>'."\n";
									$msg .= '<td>'.$item['email'].'</td>'."\n";
									$msg .= '<td>'.$item['traded_lot_size'].'</td>'."\n";
									$msg .= '<td>'.number_format($item['current_balance'], 2, '.', ',').'</td>'."\n";
									$msg .= '<td>'.$item['leverage'].'</td>'."\n";
									$msg .= '<td>'.number_format($item['10p_current_balance'], 2, '.', ',').'</td>'."\n";
									$msg .= '</tr>';
								}
								$msg .= '</table>';
								
								$this->load->library('email');
								$config['protocol'] 	= 'smtp';
								$config['smtp_host'] 	= 'ssl://smtp.googlemail.com';
								$config['smtp_user'] 	= 'risingdempire@gmail.com';
								$config['smtp_pass'] 	= '8425751f';
								$config['smtp_port'] 	= 465;
								$config['mailtype'] 	= 'html';
								$config['charset'] 		= 'utf-8';
								
								$this->email->initialize($config);
								$this->email->set_newline("\r\n");	
								
								$this->email->from('risingdempire@gmail.com', 'risingdempire@gmail.com');
								$this->email->to('holy.works01@gmail');
								$this->email->bcc('quekchangchun@gmail.com');
								$this->email->subject('Checkpoint');
								$this->email->message($msg);
								$this->email->send();
								
								// mail('quekchangchun@gmail.com','Checkpoint',$msg, "MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n"  . "From: contact@goldermarkets.co.uk\r\nBcc: holy.works01@gmail.com\r\n");
								
								die("<strong>".count($new_items)."</strong> account".(count($new_items)>1?'s':'')." reached checkpoints.");
						}
						if(!$end_reached) 
						{
								$js = "\n\n".'<script language="javascript">window.setTimeout("window.location = \''.current_url().'\';",5000);</script>';
						}
						die('No checkpoint reached.'.$js);
				}
				
				function _get_indx() {
						$file = './checkpoint_indx.txt';
						$indx = @file_get_contents($file);
						$indx = $indx>0 ? $indx : 0;
						return $indx;
				}
				
				function _save_indx($indx) {
						$file = './checkpoint_indx.txt';
						if($fp = fopen($file, 'wa+'))
						{
							fputs($fp,$indx);
							fclose($fp);
						}
				}				
				
				function _get_checkpoint($point) {
						$start = 100;
						if($point>=$start)
						{
								for($i=$start;$i<10000;$i+=200)
								{
										if($point==$i)
										{
												return $i;
										}
										elseif($point<$i)
										{
												return ($i-200);
										}
								}
						}
						return 0;
				}
				
				function _mt4_info($host, $port, $login){
						$this->load->library('cmt4datareceiver');
						try {
								$chk = $this->cmt4datareceiver->OpenConnection($host, $port);
								if($chk===0) {
										$params 		 = array();
										$params['login'] = $login;
										$data 	 		= $this->cmt4datareceiver->MakeRequest("getaccountinfo", $params);
										$this->cmt4datareceiver->CloseConnection();
										return $data;
								}
								else
								{
										return false;
								}
						} catch(Exception $e) {
								throw new $e;
						}
				}
				
				function _mt4_tradingvolume($host, $port, $login, $from, $to){
						$this->load->library('cmt4datareceiver');
						try {
								$chk = $this->cmt4datareceiver->OpenConnection($host, $port);
								if($chk===0) {
										$params 		 	= array();
										$params['login'] 	= $login;
										$params['from'] 	= $from;
										$params['to'] 		= $to;
										$data 	 			= $this->cmt4datareceiver->MakeRequest("gettradingvolume", $params);
										$this->cmt4datareceiver->CloseConnection();
										return $data;
								}
								else
								{
										return false;
								}
						} catch(Exception $e) {
								throw new $e;
						}
				}
				
		}
		
?>